package org.pupil.rpc.tolerant.FaultTolerantStrategyImpl;

import lombok.extern.slf4j.Slf4j;
import org.pupil.rpc.protocol.RpcResponse;
import org.pupil.rpc.tolerant.FaultTolerantStrategy;
import org.pupil.rpc.tolerant.FaultTolerantStrategyShareContainer;

/**
 * 快速失败
 */
@Slf4j
public class FailFastFaultTolerantStrategy implements FaultTolerantStrategy {
    @Override
    public void handler() {
        // 获取容错策略数据共享容器
        FaultTolerantStrategyShareContainer shareContainer = FaultTolerantStrategyShareContainer.getInstance();
        // 错误信息
        String errorMsg = shareContainer.getErrorMsg();

        log.warn("rpc 调用失败, 触发 FAilFast 策略, 异常信息：{}", errorMsg);
        // 设置有返回值
        shareContainer.setIsReturnValue(true);
        // 获取响应消息
        RpcResponse response = shareContainer.getRpcResponse();
        // 设置是否有返回值
        shareContainer.setIsReturnValue(true);
        // 设置返回值
        shareContainer.setReturnValue(response.getException());
    }
}
